export default (props: any) => {
    const { $global } = props.ujt;

    function getPageList(pageSize: number) {
        // 解析查询参数并设置默认值
        const totalItems = Number($global.location.query.pc) || 0;
        const totalPages =
            totalItems === 0 ? 0 : Math.ceil(totalItems / pageSize);

        // 处理无数据情况
        if (totalPages === 0) return [];

        // 规范化当前页码
        let currentPage = Math.min(
            Math.max(Number($global.location.query.pg) || 1, 1),
            totalPages
        );

        // 计算分页起始位置
        let startPage = Math.max(currentPage - Math.floor(pageSize / 2), 1);
        let endPage = startPage + pageSize - 1;

        // 调整尾部越界情况
        if (endPage > totalPages) {
            endPage = totalPages;
            startPage = Math.max(endPage - pageSize + 1, 1);
        }

        // 生成页码数组
        return Array.from(
            { length: endPage - startPage + 1 },
            (_, index) => startPage + index
        );
    }
    return { getPageList };
};
